package com.ruoyi.web.controller.clientinfo;

import java.util.List;

import com.ruoyi.system.domain.TransactionTable;
import com.ruoyi.system.service.ITransactionTableService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ClientInfo;
import com.ruoyi.system.service.IClientInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 客户信息Controller
 *
 * @author 杨琛
 * @date 2021-04-26
 */
@Controller
@RequestMapping("/system/info")
public class ClientInfoController extends BaseController {
    private String prefix = "system/info";

    @Autowired
    private IClientInfoService clientInfoService;

    @Resource
    private ITransactionTableService transactionTableService;

    @RequiresPermissions("system:info:view")
    @GetMapping()
    public String info() {
        return prefix + "/info";
    }

    /**
     * 查询客户信息列表
     */
    @RequiresPermissions("system:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ClientInfo clientInfo) {
        startPage();
        List<ClientInfo> list = clientInfoService.selectClientInfoList(clientInfo);
        return getDataTable(list);
    }

    /**
     * 导出客户信息列表
     */
    @RequiresPermissions("system:info:export")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ClientInfo clientInfo) {
        List<ClientInfo> list = clientInfoService.selectClientInfoList(clientInfo);
        ExcelUtil<ClientInfo> util = new ExcelUtil<ClientInfo>(ClientInfo.class);
        return util.exportExcel(list, "客户信息数据");
    }

    /**
     * 新增客户信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存客户信息
     */
    @RequiresPermissions("system:info:add")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ClientInfo clientInfo) {
        return toAjax(clientInfoService.insertClientInfo(clientInfo));
    }

    /**
     * 修改客户信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ClientInfo clientInfo = clientInfoService.selectClientInfoById(id);
        mmap.put("clientInfo", clientInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存客户信息
     */
    @RequiresPermissions("system:info:edit")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ClientInfo clientInfo) {
        return toAjax(clientInfoService.updateClientInfo(clientInfo));
    }

    /**
     * 删除客户信息
     */
    @RequiresPermissions("system:info:remove")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(clientInfoService.deleteClientInfoByIds(ids));
    }


    @RequiresPermissions("system:selectinfo:view")
    @GetMapping("/selectinfo/{id}")
    public String infos(@PathVariable("id") Long id, ModelMap map) {
        map.put("id", id);
        return prefix + "/infos";
    }

    /**
     * 查询客户信息列表
     */
    @RequiresPermissions("system:info:selectinfo")
    @PostMapping("/selectinfo")
    @ResponseBody
    public TableDataInfo selectInfo(Integer id) {
        TransactionTable transactionTable = transactionTableService.selectTransactionTableById(id);
        startPage();
        List<ClientInfo> list = clientInfoService.selectTable(transactionTable.getArea());
        return getDataTable(list);
    }


}
